package com.songlanyun.modules.officialDonation.controller.admin;

import com.songlanyun.common.enums.DonationConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.Constant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.exception.DonationExceptionEnum;
import com.songlanyun.modules.officialDonation.entity.OfficialDonationEntity;
import com.songlanyun.modules.officialDonation.service.OfficialDonationService;
import com.songlanyun.modules.publicbenefitmsg.model.to.PublicBenefitMsgTO;
import com.songlanyun.modules.publicbenefitmsg.service.PublicBenefitMsgService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 官方捐赠
 *
 * @author wuchao
 * @email songlanyun@gmail.com
 * @date 2022-08-02 17:56:54
 */
@RestController
@RequestMapping("/admin/official/donation")
public class OfficialDonationAdminController extends AbstractController {
    @Autowired
    private OfficialDonationService officialDonationService;

    @Resource
    private PublicBenefitMsgService publicBenefitMsgService;

    /**
     * 列表
     */
    @GetMapping("/page")
    //@RequiresPermissions(":officialdonation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = officialDonationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions(":officialdonation:info")
    public R info(@PathVariable("id") Long id) {
        OfficialDonationEntity officialDonation = officialDonationService.getById(id);

        return R.ok().put("data", officialDonation);
    }

    /**
     * 审核
     */
    @PostMapping("/audit")
    //@RequiresPermissions(":officialdonation:save")
    public R save(@RequestBody OfficialDonationEntity officialDonation) {
        ValidatorUtils.validateEntity(officialDonation, UpdateGroup.class);
        officialDonation.setAuditor(getUserId());
        OfficialDonationEntity entity = officialDonationService.audit(officialDonation);

        //审核通过
        if (Constant.AUDITPASS == entity.getAuditStatus()) {
            //发送公益消息
            publicBenefitMsgService.sendMsg(
                    PublicBenefitMsgTO.builder()
                            .type(DonationConstant.MsgType.DONATE)
                            .modelType(DonationConstant.DonateMsgType.OFFICIAL_DONATION.getCode())
                            .modelId(entity.getId()).build()
            );
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    //@RequiresPermissions(":officialdonation:delete")
    public R delete(@PathVariable("id") Long id) {
        OfficialDonationEntity entity = officialDonationService.loadById(id, true);
        //审核通过不能删除
        if (Constant.PASS == entity.getAuditStatus()) {
            throw new RRException(DonationExceptionEnum.CANNOT_BE_DELETED_IF_APPROVED);
        }
        officialDonationService.removeById(id);

        return R.ok();
    }

}
